<script setup>
import { ref, onMounted } from 'vue'
import toolApi from '@/api/tool'
import request from "@/utlis/request.js";

const baseUrl = request.defaults.baseURL + "/images/";

// 工具数据
const toolsByCategory = ref({})
const keyword = ref('')
const searchResults = ref([])

const fetchTools = async () => {
  const toolList = await toolApi.getListWithCategoryName()
  toolsByCategory.value = {}
  toolList.forEach(tool => {
    if (!toolsByCategory.value[tool.categoryName]) {
      toolsByCategory.value[tool.categoryName] = []
    }
    toolsByCategory.value[tool.categoryName].push(tool)
  })
}

const handleSearch = async () => {
  if (!keyword.value.trim()) {
    searchResults.value = []
    return
  }
  const res = await toolApi.search(keyword.value)
  searchResults.value = res
}

onMounted(() => {
  fetchTools()
})
</script>

<template>
  <div class="tool-page">
    <!-- 顶部区域 -->
    <div class="top-bar">
      <h1 class="page-title">工具合集</h1>
      <div class="search-area">
        <el-input
            v-model="keyword"
            placeholder="请输入关键词搜索"
            style="width: 300px; margin-right: 10px;"
            @keyup.enter="handleSearch"
            clearable
        />
        <el-button type="primary" @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <!-- 内容区域 -->
    <div class="content-area">
      <!-- 搜索结果 -->
      <div v-if="searchResults.length" class="tool-list">
        <div v-for="tool in searchResults" :key="tool.toolId" class="tool-card">
          <img :src="baseUrl + tool.icon" alt="icon" class="tool-icon" />
          <h4 class="tool-name">{{ tool.toolName }}</h4>
          <p class="tool-category">分类：{{ tool.categoryName }}</p>
          <a :href="tool.link" target="_blank" class="tool-link">打开工具</a>
        </div>
      </div>

      <!-- 分组列表 -->
      <div v-else>
        <div v-for="(tools, category) in toolsByCategory" :key="category" class="category-block">
          <h2 class="category-title">{{ category }}</h2>
          <div class="tool-list">
            <div v-for="tool in tools" :key="tool.toolId" class="tool-card">
              <img :src="baseUrl + tool.icon" alt="icon" class="tool-icon" />
              <h4 class="tool-name">{{ tool.toolName }}</h4>
              <a :href="tool.link" target="_blank" class="tool-link">打开工具</a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.tool-page {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-width: 100vw;
  padding: 20px 40px;
  background: #f5f7fa;
  box-sizing: border-box;
}

/* 顶部栏 */
.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

/* 标题 */
.page-title {
  font-size: 28px;
  font-weight: bold;
  color: #333;
}

/* 搜索区域 */
.search-area {
  display: flex;
  align-items: center;
}

/* 内容区 */
.content-area {
  flex: 1;
  background: #fff;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow-y: auto;
}

/* 分类块 */
.category-block {
  margin-bottom: 40px;
}

/* 分类标题 */
.category-title {
  font-size: 22px;
  color: #666;
  margin-bottom: 20px;
}

/* 工具列表 */
.tool-list {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

/* 工具卡片 */
.tool-card {
  width: 220px;
  padding: 15px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  background: #fafafa;
  text-align: center;
  transition: all 0.3s;
}

.tool-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* 工具图标 */
.tool-icon {
  width: 40px;
  height: 40px;
  margin-bottom: 10px;
}

/* 工具名字 */
.tool-name {
  font-size: 18px;
  margin: 10px 0;
  color: #333;
}

/* 工具分类 */
.tool-category {
  font-size: 14px;
  color: #999;
}

/* 工具链接 */
.tool-link {
  display: inline-block;
  margin-top: 8px;
  font-size: 14px;
  color: #409EFF;
}
.tool-link:hover {
  text-decoration: underline;
}
</style>
